package controller

import (
	"context"
	v1 "itam/api/v1"
	"itam/internal/model"
	"itam/internal/service"
)

type cEnv struct{}

var Env = cEnv{}

func (c *cEnv) Create(ctx context.Context, req *v1.EnvCreateReq) (res *v1.EnvCreateRes, err error) {
	out, err := service.Env().Create(ctx, model.EnvCreateInput{
		Name: req.Name,
	})
	if err != nil {
		return nil, err
	}
	return &v1.EnvCreateRes{Id: out.Id}, nil
}

func (c *cEnv) Delete(ctx context.Context, req *v1.EnvDeleteReq) (res *v1.EnvDeleteRes, err error) {
	err = service.Env().Delete(ctx, req.Id)
	return
}

func (c *cEnv) GetList(ctx context.Context, req *v1.EnvGetListReq) (res *v1.EnvGetListRes, err error) {
	out, err := service.Env().GetList(ctx, model.EnvGetListInput{
		Name: req.Name,
		Page: req.Page,
		Size: req.Size,
	})
	if err != nil {
		return nil, err
	}
	return &v1.EnvGetListRes{
		List:  out.List,
		Page:  out.Page,
		Size:  out.Size,
		Total: out.Total,
	}, nil
}

func (c *cEnv) Detail(ctx context.Context, req *v1.EnvDetailReq) (res *v1.EnvDetailRes, err error) {
	out, err := service.Env().Detail(ctx, req.Id)
	if err != nil {
		return nil, err
	}
	return &v1.EnvDetailRes{
		VmList:         out.VmList,
		MiddlewareList: out.MiddlewareList,
	}, nil
}
